package com.etone.smartAudit.controller;

import com.etone.smartAudit.annotation.AdminOperation;
import com.etone.smartAudit.annotation.LoginAdmin;
import com.etone.smartAudit.domain.Admin;
import com.etone.smartAudit.domain.Item;
import com.etone.smartAudit.dto.item.ItemDTO;
import com.etone.smartAudit.dto.item.ItemSourceDTO;
import com.etone.smartAudit.enums.ItemSourceType;
import com.etone.smartAudit.service.ItemService;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.item.*;
import com.etone.smartAudit.vo.serverPath.ServerPathVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.List;

/**
 * @author: 蒋学伟
 * @create: 2020-07-16 10:58:51
 * @Description ItemController
 */

@AllArgsConstructor
@RestController
@Api(tags = "项目管理", value = "ItemController")
public class ItemController {

    private ItemService itemService;

    @ApiOperation(value = "项目列表《完》")
    @GetMapping("/items")
    public PagerVO<ItemListVO> listItemVOList(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize,
            @ApiParam(value = "关键字(项目名称，负责人，事务所)") @RequestParam(required = false) String keys,
            @ApiParam(value = "创建时间-开始时间") @RequestParam(required = false) Date startCreateTime,
            @ApiParam(value = "创建时间-结束时间") @RequestParam(required = false) Date endCreateTime
    ) {
        return itemService.findItemVOList(admin, keys, startCreateTime, endCreateTime, page, pageSize);
    }

    @ApiOperation(value = "项目基本信息列表《完》")
    @GetMapping("/items/simple")
    public PagerVO<ItemSimpleListVO> listItemVOSimpleList(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "是否查询所有项目") @RequestParam(required = false, defaultValue = "false") Boolean all,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize,
            @ApiParam(value = "关键字(项目名称)") @RequestParam(required = false) String keys
    ) {
        return itemService.listItemVOSimpleList(admin, all, keys, page, pageSize);
    }


    @ApiOperation(value = "获取当前登录用户拥有的项目")
    @GetMapping("/items/selfItems")
    public List<ItemSimpleListVO> listSlefItem(
            @ApiIgnore @LoginAdmin Admin admin
    ) {
        return itemService.getSelfItems(admin);
    }

    @ApiOperation(value = "查询正在进行的项目《完》")
    @GetMapping("/items/simple/ongoing")
    public PagerVO<ItemSimpleListVO> listItemOngoing(
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return itemService.listItemOngoing(page, pageSize);
    }

    @ApiOperation(value = "项目详情基本信息《完》")
    @GetMapping("/items/{id}/simple")
    public ItemBaseVO detailSimple(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return itemService.detailSimple(id);
    }

    @ApiOperation(value = "项目详情《完》")
    @GetMapping("/items/{id}")
    public ItemDetailVO detail(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return itemService.detail(id);
    }

    @AdminOperation(module = "项目管理", item = "添加")
    @ApiOperation(value = "添加项目《完》")
    @PostMapping("/items")
    public Integer add(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "项目表单") @Validated @RequestBody ItemDTO itemDTO
    ) {
        return itemService.add(admin, itemDTO);
    }

    @AdminOperation(module = "项目管理", item = "编辑")
    @ApiOperation(value = "编辑项目《完》")
    @PutMapping("/items/{id}")
    public void edit(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "id", required = true) @PathVariable Integer id,
            @ApiParam(value = "项目表单") @Validated @RequestBody ItemDTO itemDTO
    ) {
        itemService.edit(id, itemDTO, admin);
    }

    @ApiOperation(value = "项目资源基本信息列表《完》")
    @GetMapping("/items/sources/simple")
    public List<ItemSourceBaseVO> itemSourceSimple(
            @ApiParam(value = "类型") @RequestParam(required = false) ItemSourceType type,
            @ApiParam(value = "项目id") @RequestParam(required = false) Integer itemId,
            @ApiParam(value = "服务器id") @RequestParam(required = false) Integer serverId
    ) {
        return itemService.listItemSourceSimple(type, itemId, serverId);
    }

    @ApiOperation(value = "项目资源列表《完》")
    @GetMapping("/items/sources")
    public PagerVO<ItemSourceVO> itemSource(
            @ApiParam(value = "项目资源名称") @RequestParam(required = false) String name,
            @ApiParam(value = "类型") @RequestParam(required = false) ItemSourceType type,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return itemService.listItemSource(name, type, page, pageSize);
    }

    @ApiOperation(value = "项目资源申请总情况《完》")
    @GetMapping("/items/{id}/sources/apply/total")
    public List<ItemSourceUsedVO> itemSourceApplyTotal(
            @ApiParam(value = "id", required = true) @PathVariable Integer id
    ) {
        return itemService.itemSourceApplyTotal(id);
    }

    @ApiOperation(value = "项目资源详情《完》")
    @GetMapping("/items/sources/{itemSourceId}")
    public ItemSourceDetailVO itemSourceDetail(
            @ApiParam(value = "项目资源id", required = true) @PathVariable Integer itemSourceId
    ) {
        return itemService.itemSourceDetail(itemSourceId);
    }

    @ApiOperation(value = "编辑项目资源《完》")
    @PutMapping("/items/sources/{itemSourceId}")
    public void editItemSource(
            @ApiParam(value = "项目资源id", required = true) @PathVariable Integer itemSourceId,
            @ApiParam(value = "表单详情") @RequestBody @Validated ItemSourceDTO itemSourceDTO
    ) {
        itemService.editItemSource(itemSourceId, itemSourceDTO);
    }

    @ApiOperation(value = "统计项目资源项目个数《完》")
    @GetMapping("/items/sources/analysisCount")
    public List<ItemSourceUsedTotal> analysisSourceItemCount() {
        return itemService.analysisSourceItemCount();
    }

    @ApiOperation(value = "通过项目资源查询正在使用的项目列表《完》")
    @GetMapping("/items/names/sources")
    public List<String> findItemNameBySource(
            @ApiParam(value = "项目资源id") @RequestParam Integer itemSourceId
    ) {
        return itemService.findItemNameBySource(itemSourceId);
    }

    @AdminOperation(module = "项目管理", item = "删除")
    @ApiOperation(value = "删除项目《完》")
    @DeleteMapping("/items/{id}")
    public void delete(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "项目id", required = true) @PathVariable Integer id
    ) {
        itemService.delete(id, admin);
    }

    @AdminOperation(module = "项目管理", item = "批量删除")
    @ApiOperation(value = "批量删除项目《完》")
    @DeleteMapping("/items/batch/{ids}")
    public void batchDelete(
            @ApiIgnore @LoginAdmin Admin admin,
            @ApiParam(value = "项目id", required = true) @PathVariable List<Integer> ids
    ) {
        itemService.batchDelete(ids, admin);
    }

    @ApiOperation(value = "统计持续审计和专项审计数量《完》", notes = "持续审计表示：项目类型为持续审计的项目,专项审计表示：项目类型为专项审计的项目，其他：项目类型除了专项审计和持续审计外的其他项目")
    @GetMapping("/items/types/cnt")
    public ItemTypeCntVO analysisItemTypeCnt() {
        return itemService.analysisItemTypeCnt();
    }

    @ApiOperation(value = "项目资源关系《完》")
    @GetMapping("/items/resourceRelations")
    public List<ItemResourceRelation> resourceRelations() {
        return itemService.resourceRelations();
    }

    @ApiOperation(value = "项目硬件资源信息《完》")
    @GetMapping("/items/{id}/hardware")
    public PagerVO<ServerPathVO> listItemHardware(
            @ApiParam(value = "项目id", required = true) @PathVariable Integer id,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return itemService.listItemHardware(id, page, pageSize);
    }

    @ApiOperation(value = "项目软件资源信息《完》")
    @GetMapping("/items/{id}/softwares")
    public PagerVO<ItemSoftwareVO> listItemSoftware(
            @ApiParam(value = "项目id", required = true) @PathVariable Integer id,
            @ApiParam(value = "当前页") @RequestParam(required = false, defaultValue = "1") int page,
            @ApiParam(value = "每页显示数量") @RequestParam(required = false, defaultValue = "10") int pageSize
    ) {
        return itemService.listItemSoftware(id, page, pageSize);
    }
}
